package com.km.team.phsystem.web.controller;

import com.km.team.phsystem.beans.pojo.SystemUser;
import com.km.team.phsystem.beans.pojo.SystemUserExample;
import com.km.team.phsystem.beans.vo.PageBean;
import com.km.team.phsystem.beans.vo.ResponseData;
import com.km.team.phsystem.beans.vo.SystemUserVo;
import com.km.team.phsystem.common.constants.Constants;
import com.km.team.phsystem.common.enums.ResultEnum;
import com.km.team.phsystem.common.utils.StrUtils;
import com.km.team.phsystem.web.controller.base.BaseController;
import com.km.team.phsystem.web.utils.ResponseDataUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * Author: jiangjun
 * Date: 2018/10/14
 * Time: 下午4:13
 * Description:
 */
@RestController
@RequestMapping("/user")
public class SystemUserController extends BaseController<SystemUserController> {

    @PostMapping("/update")
    public ResponseData update(@RequestBody SystemUser user) throws Exception {
        if (user == null) {
            ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        systemUserService.update(user);
        return ResponseDataUtil.buildSuccess();
    }

    @PostMapping(value = "/queryPagedData", produces = "application/json;charset=UTF-8")
    public ResponseData queryPagedData(@RequestBody SystemUserVo vo) throws Exception {
        if (vo == null) {
            return null;
        }
        PageBean pageBean = systemUserService.getPagedData(vo);
        // 返回分页数据
        return ResponseDataUtil.buildSuccess(pageBean);
    }

    @ApiOperation(value = "新增用户", notes = "")
    @PostMapping("/add")
    public ResponseData add(@RequestBody @Valid SystemUserVo vo, BindingResult bindingResult) throws Exception {
        if(bindingResult.hasErrors()){
            return ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        //判断新增用户是否与现有用户重名
        SystemUserExample systemUserExample = new SystemUserExample();
        systemUserExample.or().andUserNameEqualTo(vo.getUserName());
        List<SystemUser> systemUsersList = systemUserService.getUser(systemUserExample);
        if (!CollectionUtils.isEmpty(systemUsersList)) {
            return ResponseDataUtil.buildError(Constants.ERROR, "指定的用户已存在！");
        }
        int i = systemUserService.add(vo);
        return ResponseDataUtil.buildSuccess();
    }

    @ApiOperation(value = "用户登录", notes = "")
    @PostMapping("/login")
    public ResponseData login(@RequestBody SystemUserVo vo, HttpSession session) throws Exception {
        if (vo == null) {
            ResponseDataUtil.buildError(ResultEnum.PARAM_ERROR);
        }
        SystemUserExample systemUserExample = new SystemUserExample();
        systemUserExample.or().andUserNameEqualTo(vo.getUserName()).andPasswordEqualTo(StrUtils.getMD5(vo.getPassword()));
        List<SystemUser> systemUsersList = systemUserService.getUser(systemUserExample);
        if (systemUsersList.size() == 1)
            //保存登录成功的用户至session
            session.setAttribute("user",systemUsersList.get(0));
        else
            return ResponseDataUtil.buildError(Constants.ERROR, "用户名或密码错误！");

        return ResponseDataUtil.buildSuccess();
    }

}

